package com.journaldev.thread.chenhao;

/**
 * 线程的通信
 *
 * @author: chenhs
 * @date: Created in 16:01 2019/9/4
 **/
public class PrintNum implements Runnable {

    int num = 1;

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                /**唤醒wait的一个线程*/
                notify();
                if (num > 100) {
                    break;
                } else {

                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " : " + num++);

                }

                try {
                    /**释放当前的锁*/
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


        }
    }
}
